home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / time / unixclock.lha / unixclock / unixclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  2.4 KB  |  96 lines

  1. /*
  2.  *        unixclock - make the amiga hardware clock behave like a UNIX clock
  3.  *
  4.  *        © Copyright 1995 by Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  5.  *
  6.  *        -----------------------------------------------------------------------------
  7.  *
  8.  *        This file is subject to the terms and conditions of the GNU General Public
  9.  *        License.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <dos/dos.h>
  14. #include <devices/timer.h>
  15. #include <libraries/locale.h>
  16. #include <proto/exec.h>
  17. #include <proto/locale.h>
  18. #include <proto/battclock.h>
  19.  
  20.  
  21. #define LVOReadBattClock    (-12)
  22. #define LVOWriteBattClock    (-18)
  23.  
  24.  
  25. struct Node *BattClockBase;
  26. struct Device *TimerBase;
  27. struct MsgPort *TimerPort;
  28. struct timerequest *TimerRequest;
  29. struct Locale *Locale;
  30. ULONG GMTOffset;
  31.  
  32.  
  33. APTR OldReadBattClock;
  34. APTR OldWriteBattClock;
  35.  
  36.  
  37. asm("
  38.     .text
  39.     .globl _NewReadBattClock
  40.     .globl _NewWriteBattClock
  41.  
  42. _NewReadBattClock:
  43.     move.l    (_OldReadBattClock),a0
  44.     jsr        (a0)
  45.     sub.l        (_GMTOffset),d0
  46.     rts
  47.  
  48. _NewWriteBattClock:
  49.     add.l        (_GMTOffset),d0
  50.     move.l    (_OldWriteBattClock),a0
  51.     jmp        (a0)
  52. ");
  53.  
  54.  
  55. extern ULONG NewReadBattClock(void);
  56. extern void NewWriteBattClock(ULONG);
  57.  
  58.  
  59. void SetClock(void)
  60. {
  61.     TimerRequest->tr_node.io_Command = TR_SETSYSTIME;
  62.     TimerRequest->tr_node.io_Flags = IOF_QUICK;
  63.     TimerRequest->tr_time.tv_micro = 0;
  64.     TimerRequest->tr_time.tv_secs = ReadBattClock();
  65.     DoIO((struct IORequest *)TimerRequest);
  66. }
  67.  
  68.  
  69. int main(int argc, char *argv[])
  70. {
  71.     Locale = OpenLocale(NULL);
  72.     GMTOffset = 60*Locale->loc_GMTOffset;
  73.     BattClockBase = OpenResource("battclock.resource");
  74.     if (TimerPort = CreateMsgPort()) {
  75.         if (TimerRequest = CreateIORequest(TimerPort, sizeof(struct timerequest))) {
  76.             if (!OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)TimerRequest, NULL)) {
  77.                 TimerBase = TimerRequest->tr_node.io_Device;
  78.                 OldReadBattClock = SetFunction((struct Library *)BattClockBase, LVOReadBattClock,
  79.                                                          NewReadBattClock);
  80.                 OldWriteBattClock = SetFunction((struct Library *)BattClockBase, LVOWriteBattClock,
  81.                                                           (ULONG (*)())NewWriteBattClock);
  82.                 SetClock();
  83.                 SetSignal(NULL, SIGBREAKF_CTRL_C);
  84.                 Wait(SIGBREAKF_CTRL_C);
  85.                 SetFunction((struct Library *)BattClockBase, LVOReadBattClock, OldReadBattClock);
  86.                 SetFunction((struct Library *)BattClockBase, LVOWriteBattClock, OldWriteBattClock);
  87.                 SetClock();
  88.                 CloseDevice((struct IORequest *)TimerRequest);
  89.             }
  90.             DeleteIORequest(TimerRequest);
  91.         }
  92.         DeleteMsgPort(TimerPort);
  93.     }
  94.     exit(0);
  95. }
  96.